package com.dzx.awt;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年07月22日 11:15:37
 */
public class HandDraw {
    private Frame f = new Frame("paint"); //创建窗体
    private final int AREA_WIDTH = 500;  //宽
    private final int AREA_HEIGHT = 400;  //高
    private int preX = -1;  //记录上一个坐标位置
    private int preY = -1;
    //创建一个右键菜单
    PopupMenu pop = new PopupMenu();
    MenuItem redItem = new MenuItem("red");
    MenuItem greenItem = new MenuItem("green");
    MenuItem blueItem = new MenuItem("blue");

    private Color foreColor = new Color(255, 0, 0);
    //定义一个BufferedImage对象
    BufferedImage bufferedImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
    //获取对象的
    Graphics g = bufferedImage.getGraphics();

    private DrawCanvas drawArea = new DrawCanvas();

    public void init() {
        //组合成右键菜单
        pop.add(blueItem);
        pop.add(greenItem);
        pop.add(redItem);
        myEvent();
        drawArea.add(pop);//将右键菜单加入到画布中
        g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT);//将对象的初始颜色填为白的
        drawArea.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT));//设置大小
        /*
         * Dimension：封装组件中的宽度和高度
         */
        f.add(drawArea);
        f.pack();
        f.setVisible(true);
    }

    private void myEvent() {
        //添加右键菜单的监听器
        redItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                foreColor = new Color(255, 0, 0);
            }
        });
        blueItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                foreColor = new Color(0, 0, 255);
            }
        });
        greenItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                foreColor = new Color(0, 255, 0);
            }
        });
        drawArea.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {   //推拽鼠标事件监听
                if (preX > 0 && preY > 0) {
                    g.setColor(foreColor);
                    g.drawLine(preX, preY, e.getX(), e.getY());
                }
                preX = e.getX();
                preY = e.getY();
                drawArea.repaint();
            }

            @Override
            public void mouseMoved(MouseEvent e) {
            }
        });

        drawArea.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {  //松开鼠标事件监听
                if(e.isPopupTrigger()){
                 pop.show(drawArea, e.getX(), e.getY());
                 preX = -1;
                 preY = -1;
                }
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                if (preX > 0 && preY > 0) {
                    g.setColor(foreColor);
                    g.drawLine(preX, preY, e.getX(), e.getY());
                }
                preX = e.getX();
                preY = e.getY();
                drawArea.repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }


            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    public static void main(String[] args){
      new HandDraw().init();
    }


    class DrawCanvas extends Canvas {
        public void paint(Graphics graphics) {
            graphics.drawImage(bufferedImage, 0, 0, null);
        }
    }
}
